package com.menker.app.util;


import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.menker.app.Constants;
import com.menker.app.common.SharedPreferencesHelper;
import com.menker.app.common.Utils;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.List;

/**
 * @author J.Huang
 * @version 1.0
 */
public class AppUtils {

    public static final int SCREEN_HDPI_CODE = 1;
    public static final int SCREEN_XDPI_CODE = 2;
    public static final int SCREEN_XXDPI_CODE = 3;

    public static boolean isWechatAppInstalledAndSupported(Context context) {

        IWXAPI msgApi = WXAPIFactory.createWXAPI(context, Constants.WECHAT_APP_ID, false);
        msgApi.registerApp(Constants.WECHAT_APP_ID);
        List<ApplicationInfo> list = context.getPackageManager().getInstalledApplications(0);

        for (ApplicationInfo info :
                list) {
            if ("com.tencent.mm".equals(info.packageName) && msgApi.isWXAppSupportAPI()) {
                return true;
            }
        }

        return false;
//        IWXAPI msgApi = WXAPIFactory.createWXAPI(context, Constants.WECHAT_APP_ID, false);
//        msgApi.registerApp(Constants.WECHAT_APP_ID);
//
//        return msgApi.isWXAppInstalled();
//                && msgApi.isWXAppSupportAPI();
    }

    public static void routineCheckVersion(final Activity activity, int remoteVersionCode, String downloadUrl, boolean forceUpdate, SharedPreferencesHelper sharedPrefHelper) {
        long lastCheck = sharedPrefHelper.getLong("checkUpdateTime", 0L);
        long now = System.currentTimeMillis();
        if (now - lastCheck > 12 * 60 * 60 * 1000) {    // 每12个小时检查一遍是否更新
            sharedPrefHelper.setData("checkUpdateTime", now);

            if (remoteVersionCode > Utils.App.getAppVersionCode()) {
                DialogUtils.showUpdateDialog(activity, downloadUrl, sharedPrefHelper, forceUpdate);
            }
        }
    }

    public static boolean manualCheckVersion(final Activity activity, int remoteVersionCode, String downloadUrl, boolean forceUpdate, SharedPreferencesHelper sharedPrefHelper) {
        long now = System.currentTimeMillis();

        sharedPrefHelper.setData("checkUpdateTime", now);

        if (remoteVersionCode > Utils.App.getAppVersionCode()) {
            DialogUtils.showUpdateDialog(activity, downloadUrl, sharedPrefHelper, forceUpdate);
            return true;
        } else {
            DialogUtils.showAlreadyNewestVersionDialog(activity);
            return false;
        }
    }

    /**
     * 将dp转换为px
     * @param dp
     * @param context
     * @return
     */
    public static int dp2px(int dp,Context context) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                context.getResources().getDisplayMetrics());
    }

    /**
     * 判断屏幕分辨率
     */
    public static int getScreenSize(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        // 屏幕密度（每寸像素：120/160/213/240/320/480/640）
        int densityDpi = dm.densityDpi;

        if (densityDpi == 240) {
            return SCREEN_HDPI_CODE;
        }else if (densityDpi == 320) {
            return SCREEN_XDPI_CODE;
        }else if (densityDpi == 480) {
            return SCREEN_XXDPI_CODE;
        }else {
            return 0;
        }
    }
}
